package org.fly.service.impl;

import org.codehaus.jackson.type.TypeReference;
import org.fly.bean.LevelPrice;
import org.fly.bean.SupplierProduct;
import org.fly.bean.query.SupplierProductQuery;
import org.fly.dao.SupplierProductDao;
import org.fly.service.SupplierProductService;
import org.fly.utils.JsonUtils;
import org.fly.utils.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.fly.bean.common.BusinessException;
import java.util.List;

/**
* Created by autoCode on 2018-02-07 16:53:26.
*/
@Service
public class SupplierProductServiceImpl implements SupplierProductService {

    @Autowired
    SupplierProductDao supplierProductDao;

    @Override
    public List<SupplierProduct> list(SupplierProductQuery query) {
        List<SupplierProduct>  list = supplierProductDao.list(query);
        list.forEach(e->{
            if(StringUtils.isNotBlank(e.getConfigJson())){

            }
        });
        return list;
    }

    @Override
    public long count(SupplierProductQuery query) {
        return supplierProductDao.count(query);
    }

    @Override
    public SupplierProduct findById(String id) {
        SupplierProduct bean = supplierProductDao.findById(id);

        return bean;
    }

    @Override
    public void save(SupplierProduct bean)  throws BusinessException {
        SupplierProduct dbSP=     find(bean.getSupplierId(),bean.getProductId());
        if(dbSP !=null ){
            bean.setId(dbSP.getId());
        }
        if(StringUtils.isNotBlank(bean.getId())){
             supplierProductDao.update(bean);
        }else {
            bean.setId(UUIDFactory.random());
            bean.setCreateTime(System.currentTimeMillis());
            supplierProductDao.insert(bean);
        }
    }

    @Override
    public SupplierProduct find(String supplierId, String productId) {
        SupplierProductQuery query = new SupplierProductQuery();
        query.setSupplierId(supplierId);
        query.setProductId(productId);
        List<SupplierProduct> supplierProductList = supplierProductDao.list(query);
        if (!supplierProductList.isEmpty()){
            return supplierProductList.get(0);
        }
        return null;
    }

    @Override
    public void delete(String id) {
        supplierProductDao.delete(id);
    }
}
